package org.jeecg.modules.buildingmanagement.service.impl;

import org.jeecg.modules.buildingmanagement.entity.BuildingManagement;
import org.jeecg.modules.buildingmanagement.entity.Picture;
import org.jeecg.modules.buildingmanagement.mapper.PictureMapper;
import org.jeecg.modules.buildingmanagement.mapper.BuildingManagementMapper;
import org.jeecg.modules.buildingmanagement.service.IBuildingManagementService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 房屋管理
 * @Author: jeecg-boot
 * @Date:   2019-10-09
 * @Version: V1.0
 */
@Service
public class BuildingManagementServiceImpl extends ServiceImpl<BuildingManagementMapper, BuildingManagement> implements IBuildingManagementService {

	@Autowired
	private BuildingManagementMapper buildingManagementMapper;
	@Autowired
	private PictureMapper pictureMapper;
	
	@Override
	@Transactional
	public void saveMain(BuildingManagement buildingManagement, List<Picture> pictureList) {
		buildingManagementMapper.insert(buildingManagement);
		for(Picture entity:pictureList) {
			//外键设置
			entity.setGroupId(buildingManagement.getId());
			pictureMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(BuildingManagement buildingManagement,List<Picture> pictureList) {
		buildingManagementMapper.updateById(buildingManagement);
		
		//1.先删除子表数据
		pictureMapper.deleteByMainId(buildingManagement.getId());
		
		//2.子表数据重新插入
		for(Picture entity:pictureList) {
			//外键设置
			entity.setGroupId(buildingManagement.getId());
			pictureMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		pictureMapper.deleteByMainId(id);
		buildingManagementMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			pictureMapper.deleteByMainId(id.toString());
			buildingManagementMapper.deleteById(id);
		}
	}
	
}
